package com.teamcenter.rac.massupdate.databinding;

import java.util.ArrayList;

import org.eclipse.ui.views.properties.IPropertySource;

import com.teamcenter.rac.aif.AbstractAIFOperation;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.massupdate.model.ImpactedPart;
import com.teamcenter.rac.massupdate.model.UpdateJobModel;
import com.teamcenter.rac.massupdate.ui.Messages;
import com.teamcenter.rac.massupdate.utils.MassUpdateHelper;
import com.teamcenter.rac.massupdate.utils.SourceHelper;
import com.teamcenter.rac.util.AdapterUtil;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PlatformHelper;
import com.teamcenter.services.loose.structuremanagement.MassUpdateService;
import com.teamcenter.services.loose.structuremanagement._2012_09.MassUpdate;
@SuppressWarnings({"rawtypes","unchecked"})
public class ImpactedPartPageVisibleChangeObservable extends AbstractModelObservable {
	public ImpactedPartPageVisibleChangeObservable() {
		super(Boolean.FALSE, Boolean.class);
	}

	public void doSetValue(Object paramObject) {
		if (((Boolean) paramObject).booleanValue()) {
			initializeImpactedParts(getTCSession());
		}
	}

	public void initializeImpactedParts(TCSession paramTCSession) {
		AbstractAIFOperation local1 = new AbstractAIFOperation(
				Messages.ImpactedPartPage_Msg_SearchingForImpactedParts) {
			public void executeOperation() throws Exception {
				TCComponent localTCComponent1 = null;
				TCComponent localTCComponent2 = null;
				UpdateJobModel localUpdateJobModel = ImpactedPartPageVisibleChangeObservable.this.getUpdateJobModel();
				TCSession localTCSession = (TCSession) getSession();
				IPropertySource localIPropertySource1 = localUpdateJobModel.getTargetPart();
				if (localIPropertySource1 != null) {
					localTCComponent1 = SourceHelper.getAssociatedComponent(localIPropertySource1, localTCSession);
				}
				if (localTCComponent1 == null) {
					return;
				}
				IPropertySource localIPropertySource2 = localUpdateJobModel.getChangeObject();
				if (localIPropertySource2 != null) {
					localTCComponent2 = SourceHelper.getAssociatedComponent(localIPropertySource2, localTCSession);
				}
				MassUpdate.MassUpdateAffectedInput[] arrayOfMassUpdateAffectedInput = new MassUpdate.MassUpdateAffectedInput[1];
				arrayOfMassUpdateAffectedInput[0] = new MassUpdate.MassUpdateAffectedInput();
				arrayOfMassUpdateAffectedInput[0].target = localTCComponent1;
				arrayOfMassUpdateAffectedInput[0].change = localTCComponent2;
				arrayOfMassUpdateAffectedInput[0].operation = localUpdateJobModel.getOperationType().convertToInt();
				MassUpdateService localMassUpdateService = MassUpdateService
						.getService(localTCComponent1.getSession().getSoaConnection());
				final MassUpdate.MassUpdateAffectedResponse localMassUpdateAffectedResponse = localMassUpdateService
						.massGetAffectedParents(arrayOfMassUpdateAffectedInput);
				localUpdateJobModel.getImpactedParts().getRealm().asyncExec(new Runnable() {
					public void run() {
						if (localMassUpdateAffectedResponse == null) {
							return;
						}
						UpdateJobModel localUpdateJobModel = ImpactedPartPageVisibleChangeObservable.this
								.getUpdateJobModel();
						localUpdateJobModel.clearAllImpactedParts();
						if ((localMassUpdateAffectedResponse.massUpdateAffectedOutputs == null)
								|| (localMassUpdateAffectedResponse.massUpdateAffectedOutputs.length == 0)) {
							MessageBox.post(PlatformHelper.getCurrentShell(),
									Messages.ImpactedPartPage_Msg_NoImpactedPartsFound,
									Messages.ImpactedPartPage_Title_SearchImpactedParts, 2);
						}
						ArrayList localArrayList;
						int i;
						String[] localObject;
						if ((localMassUpdateAffectedResponse.serviceData != null)
								&& (localMassUpdateAffectedResponse.serviceData.sizeOfPartialErrors() > 0)) {
							localArrayList = new ArrayList();
							for (i = 0; i < localMassUpdateAffectedResponse.serviceData.sizeOfPartialErrors(); i++) {
								int m = (localObject = localMassUpdateAffectedResponse.serviceData.getPartialError(i)
										.getMessages()).length;
								for (int k = 0; k < m; k++) {
									String str = localObject[k];
									localArrayList.add(new TCException(str));
								}
							}
							MassUpdateHelper.displayMessage(localArrayList, Messages.ErrorDialog_Title, null);
						}
						if (localMassUpdateAffectedResponse.massUpdateAffectedOutputs.length > 0) {
							MassUpdate.MassUpdateAffectedOutput[] arrayOfMassUpdateAffectedOutput;
							int j = (arrayOfMassUpdateAffectedOutput = localMassUpdateAffectedResponse.massUpdateAffectedOutputs).length;
							for (i = 0; i < j; i++) {
								MassUpdate.MassUpdateAffectedOutput _localArrayList = arrayOfMassUpdateAffectedOutput[i];
								IPropertySource localIPropertySource = (IPropertySource) AdapterUtil
										.getAdapter(_localArrayList.parent, IPropertySource.class);
								if (localIPropertySource != null) {
									ImpactedPart _localObject = new ImpactedPart(localIPropertySource);
									((ImpactedPart) _localObject).setUpdatable(_localArrayList.selectable);
									((ImpactedPart) _localObject).setComment(_localArrayList.selectableComment);
									localUpdateJobModel.addImpactedPart((ImpactedPart) _localObject);
								}
							}
						}
						localUpdateJobModel.setImpactedPartsUpToDate(true);
					}
				});
			}
		};
		paramTCSession.queueOperation(local1);
	}
}